package com.zl.zlbaseproject.ui.main;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.zl.zlbaseproject.R;
import com.zl.zlbaseproject.base.activity.BaseActivity;
import com.zl.zlbaseproject.base.fragment.BaseFragment;
import com.zl.zlbaseproject.model.net.api.ApiService;
import com.zl.zlbaseproject.model.net.bean.BaseNetBean;
import com.zl.zlbaseproject.model.net.bean.TestBean;
import com.zl.zlbaseproject.model.net.bean.TestListBean;
import com.zl.zlbaseproject.model.net.engine.RetrofitHelper;
import com.zl.zlbaseproject.model.net.engine.TransformHelper;
import com.zl.zlbaseproject.model.net.engine.intercepter.HttpResult;
import com.zl.zlbaseproject.ui.home.fragment.HomeFragment;
import com.zl.zlbaseproject.ui.me.fragment.MeFragment;
import com.zl.zlbaseproject.ui.more.fragment.MoreFragment;
import com.zl.zlbaseproject.ui.product.fragment.ProductFragment;
import com.zl.zlbaseproject.utils.MyToast;
import com.zl.zlbaseproject.widget.simple.SimpleOnTabSelectedListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


public class MainActivity extends BaseActivity {


    @BindView(R.id.layout_fragment)
    FrameLayout mLayoutFragment;
    @BindView(R.id.bottom_bar)
    BottomNavigationBar mBottomBar;


    private int mSelectedPosition = 0;

    private List<BaseFragment> mFragments = new ArrayList();
    @Override
    protected int getContentViewRes() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
        super.initView();
        initFragment();
        mBottomBar.setMode(BottomNavigationBar.MODE_FIXED);
        mBottomBar
                .setActiveColor(R.color.textColor_bule)//选中颜色 图标和文字
                .setInActiveColor(R.color.textColor4)//默认未选择颜色
                .setBarBackgroundColor(R.color.white);//默认背景色
        mBottomBar
                .addItem(new BottomNavigationItem(R.mipmap.icon_home,"首页"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_producte,"项目"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_producte,"我的"))
                .addItem(new BottomNavigationItem(R.mipmap.icon_more,"更多"))

                .setFirstSelectedPosition(0)//设置默认选择的按钮
                .initialise();
        onSelectedPosition(0);

    }

    private void initFragment() {
        mFragments.add(new HomeFragment());
        mFragments.add(new ProductFragment());
        mFragments.add(new MeFragment());
        mFragments.add(new MoreFragment());
    }

    @Override
    protected void initListerner() {
        super.initListerner();

        mBottomBar.setTabSelectedListener(new SimpleOnTabSelectedListener(){
            @Override
            public void onTabSelected(int position) {
                super.onTabSelected(position);
                onSelectedPosition(position);
            }
        });
    }


    public void onSelectedPosition(int position) {
        if(position ==0){
            getDataSuccess();
        }else if(position ==1){
            getListData();
        }

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        Fragment targetFg = mFragments.get(position);
        Fragment lastFg = mFragments.get(mSelectedPosition);
        mSelectedPosition = position;
        ft.hide(lastFg);
        if (!targetFg.isAdded()) {
            ft.add(R.id.layout_fragment, targetFg);
        }
        ft.show(targetFg);
        ft.commit();
    }

    private void getListData() {
        RetrofitHelper
                .creatApi(ApiService.class)
                .getRestaurantList()
                .compose(TransformHelper.transformListOnMain(TestListBean.class))
                .subscribe(new HttpResult<BaseNetBean<List<TestListBean>>>(MainActivity.this) {
                    @Override
                    protected void onFailed() {

                    }

                    @Override
                    protected void onSuccess(BaseNetBean<List<TestListBean>> listBaseNetBean) {
                        List<TestListBean> body = listBaseNetBean.body;
                    }
                });

    }


    private void getDataSuccess(){

      RetrofitHelper
              .creatApi(ApiService.class)
              .getRestaurantDetail()
              .compose(TransformHelper.transformOnMain(TestBean.class))
              .subscribe(new HttpResult<BaseNetBean<TestBean>>(MainActivity.this) {
                  @Override
                  protected void onFailed() {
                      MyToast.show(MainActivity.this,"========error=====");
                  }

                  @Override
                  protected void onSuccess(BaseNetBean<TestBean> testBeanBaseNetBean) {
                      TestBean body = testBeanBaseNetBean.body;
                      MyToast.show(MainActivity.this,"========success=====");
                  }
              });
    }

}
